package vincent.com.layouttest;

import android.graphics.Color;
import android.graphics.Point;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;

/**
 * Created by chenzhengwen on 2015/6/2.
 */
public class TransparentActivity extends ActionBarActivity implements View.OnTouchListener{
    private static final String TAG = "TransparentActivity";

    private ImageView img;
    private float originalX;
    private float originalY;
    private float distance;

    private Drawable drawable;

    private ImageView bgImg;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        View mView = LayoutInflater.from(this).inflate(R.layout.activity_transparent_actionbar, null);
        setContentView(mView);
        mView.setOnTouchListener(this);

//        setContentView(R.layout.activity_transparent_actionbar);

       /* Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        ActionBar actionbar = getSupportActionBar();
        actionbar.setDisplayHomeAsUpEnabled(true);
        actionbar.setDisplayShowTitleEnabled(false);*/

        img = (ImageView)findViewById(R.id.img);
        bgImg = (ImageView)findViewById(R.id.bg_img);

        drawable = this.getResources().getDrawable(R.drawable.ic_action_back);
        drawable.setColorFilter(Color.BLACK, PorterDuff.Mode.SRC_ATOP);

        img.setImageDrawable(drawable);
//        img.setAlpha(0.1f);

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case android.R.id.home:
                finish();
                return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                Log.i(TAG, "onTouch event ACTION_DOWN");

                originalX = event.getX();
                originalY = event.getY();

                return true;
            case MotionEvent.ACTION_MOVE:
                distance = event.getY() - originalY;
                int multi = (int)(distance/10);

                Log.i(TAG, "ACTION_MOVE | multi=" + multi + " | distance=" + distance);

                int color = 0;
                color = multi * 0x111111;

                drawable.setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
                img.setImageDrawable(drawable);
                return true;

            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_UP:

                Log.i(TAG, "onTouch event ACTION_CANCEL");
                originalX = 0;
                originalY = 0;
                distance = 0;

                return true;
            case MotionEvent.ACTION_SCROLL:
                Log.i(TAG, "ACTION_SCROLL");
                return true;
        }
        return false;
    }
}
